package com.lb.shopguide.ui.fragment.guide.miji;

import android.support.v4.view.ViewPager;

import com.blankj.utilcode.util.ToastUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.adapter.viewpager.AdapterMijiType;
import com.lb.shopguide.base.BaseMainFragment;
import com.lb.shopguide.entity.TutorTypeBean;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.ui.view.NormalTitleBar;
import com.lb.shopguide.util.json.JsonUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by kang on 2017/5/31.
 */

public class FragmentMijiTypes extends BaseMainFragment {
    @BindView(R.id.ntb)
    NormalTitleBar ntb;
    @BindView(R.id.tabs)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    private List<TutorTypeBean> mTutorTypeList;
    private AdapterMijiType mAdapterMijiType;



    public static FragmentMijiTypes newInstance() {
        return new FragmentMijiTypes();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_miji;
    }

    @Override
    public void initData() {
        mTutorTypeList = new ArrayList<>();
    }

    @Override
    public void initView() {
        ntb.setTitleText("秘籍");
//        ImageLoaderUtils.display(mContext, ivTeacherAvatar, AppConstant.IMAGE_URL_TEST);
        initRequest();
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {
        ((GuideMainActivity) _mActivity).onSessionOutOfDate();
    }

    @Override
    public void requestError(String error) {
        ToastUtils.showShort(error);
    }

    @Override
    public String getPageTag() {
        return FragmentMijiTypes.class.getName();
    }

    private void initRequest() {
        sendRequestGetTutorList();
    }

    private void sendRequestGetTutorList() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                JsonArray jsonTutors = JsonUtil.getJsonArrayFromMap(response.getReturnContent());
                if (jsonTutors.size() != 0) {
                    for (JsonElement obj : jsonTutors) {
                        TutorTypeBean bean = JsonUtil.getGson().fromJson(obj, TutorTypeBean.class);
                        mTutorTypeList.add(bean);
                    }
                } else {
                    // 空白页
                }
                bindType();
            }
        };
        ApiMethodGuide.getTutorList(new SimpleLbObserver(listener,otherListener),this, FragmentEvent.DESTROY_VIEW);
    }

    private void bindType() {
        mAdapterMijiType = new AdapterMijiType(getChildFragmentManager(), mTutorTypeList);
        mViewPager.setAdapter(mAdapterMijiType);
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setViewPager(mViewPager);
    }
}